using System;
using System.Collections.Generic;
using PluginFrame;

namespace PcrDataStructure
{
	[Serializable]
	public class PcrStructure : IPcrStructure
	{
		protected Guid mGuid;

		protected IPcrDataFile mPcrDataFile;

		protected List<int> mAvailableChannels;

		protected PcrSystemType mSystemType;

		protected List<ICellIDPositionMap> mCellIDPositionMaps = new List<ICellIDPositionMap>();

		protected Dictionary<string, ICellIDPositionMap> mCellPositionIDMaps = new Dictionary<string, ICellIDPositionMap>();

		protected List<IPcrExperiment> mPcrExperiments = new List<IPcrExperiment>();

		protected List<IPcrModule> mPcrModules = new List<IPcrModule>();

		protected IChannelConfiguration[] mChannelConfigurations;

		protected DateTime mCreateTime;

		protected PcrModuleState mModuleState;

		protected IPcrInstrument mPcrInstrument;

		public IPcrInstrument PcrInstrument
		{
			get
			{
				return mPcrInstrument;
			}
		}

		public IPcrDataFile PcrDataFile
		{
			get
			{
				return mPcrDataFile;
			}
			set
			{
				mPcrDataFile = value;
			}
		}

		public Guid Guid
		{
			get
			{
				return mGuid;
			}
		}

		List<int> IPcrStructure.AvailableChannels
		{
			get
			{
				return mAvailableChannels;
			}
		}

		PcrSystemType IPcrStructure.SystemType
		{
			get
			{
				return mSystemType;
			}
			set
			{
				mSystemType = value;
			}
		}

		List<IPcrExperiment> IPcrStructure.PcrExperiments
		{
			get
			{
				return mPcrExperiments;
			}
			set
			{
				mPcrExperiments = value;
			}
		}

		IApplication IPcrStructure.Application
		{
			get
			{
				return mPcrDataFile.Application;
			}
		}

		IChannelConfiguration[] IPcrStructure.ChannelConfigurations
		{
			get
			{
				return mChannelConfigurations;
			}
		}

		PcrModuleState IPcrStructure.State
		{
			get
			{
				return mModuleState;
			}
			set
			{
				mModuleState = value;
			}
		}

		DateTime IPcrStructure.CreateTime
		{
			get
			{
				return mCreateTime;
			}
			set
			{
				mCreateTime = value;
			}
		}

		IPcrInstrument IPcrStructure.PcrInstrument
		{
			get
			{
				return mPcrInstrument;
			}
		}

		List<IPcrModule> IPcrStructure.PcrModules
		{
			get
			{
				return mPcrModules;
			}
			set
			{
				mPcrModules = value;
			}
		}

		public PcrStructure(IPcrDataFile pcrDataFile, Dictionary<int, ICellIDPositionMap> cellIDPositionMapDictionary, IChannelConfiguration[] channelConfigurations)
		{
			mPcrDataFile = pcrDataFile;
			mPcrDataFile.AddPcrStructure(this);
			mPcrDataFile.CurrentPcrStructure = this;
			mSystemType = PcrSystemType.AbsoluteAnalysis;
			IEnumerator<ICellIDPositionMap> enumerator = cellIDPositionMapDictionary.Values.GetEnumerator();
			while (enumerator.MoveNext())
			{
				ICellIDPositionMap current = enumerator.Current;
				if (current != null)
				{
					mCellIDPositionMaps.Add(current);
					mCellPositionIDMaps.Add(current.CellPosition(), current);
				}
			}
			mGuid = Guid.NewGuid();
			mChannelConfigurations = channelConfigurations;
			mPcrInstrument = new PcrInstrument(this);
		}

		public List<ISubModule> GetAvailablePcrSubModules()
		{
			IConfigurationService configurationService = (IConfigurationService)mPcrDataFile.Application.GetService("ConfigurationService");
			List<int>[] array = configurationService.InstrumentSubModules[configurationService.DefaultInstrumentType];
			List<ISubModule> list = new List<ISubModule>();
			List<ISubModule> list2 = new List<ISubModule>();
			foreach (IPcrModule mPcrModule in mPcrModules)
			{
				list.AddRange(mPcrModule.SubModules);
			}
			foreach (IPcrModule mPcrModule2 in mPcrModules)
			{
				foreach (ISubModule subModule in mPcrModule2.SubModules)
				{
					if (subModule.IsAvailable())
					{
						continue;
					}
					bool flag = false;
					int i;
					for (i = 0; i < array.Length; i++)
					{
						if (array[i].Contains(subModule.ID))
						{
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						continue;
					}
					for (int j = 0; j < list.Count; j++)
					{
						if (array[i].Contains(list[j].ID))
						{
							list2.Add(list[j]);
						}
					}
				}
			}
			foreach (ISubModule item in list2)
			{
				if (list.Contains(item))
				{
					list.Remove(item);
				}
			}
			return list;
		}

		public List<ISubModule> GetAllPcrSubModule()
		{
			List<ISubModule> list = new List<ISubModule>();
			foreach (IPcrModule mPcrModule in mPcrModules)
			{
				foreach (ISubModule subModule in mPcrModule.SubModules)
				{
					list.Add(subModule);
				}
			}
			return list;
		}

		bool IPcrStructure.Analyze()
		{
			foreach (IPcrExperiment mPcrExperiment in mPcrExperiments)
			{
				mPcrExperiment.Analyze();
			}
			return true;
		}

		void IPcrStructure.AddPcrModule(IPcrModule pcrModule)
		{
			if (!mPcrModules.Contains(pcrModule))
			{
				mPcrModules.Add(pcrModule);
			}
		}

		void IPcrStructure.RemovePcrModule(IPcrModule pcrModule)
		{
			if (mPcrModules.Contains(pcrModule))
			{
				mPcrModules.Remove(pcrModule);
			}
		}

		IPcrModule IPcrStructure.GetPcrModule(int index)
		{
			return mPcrModules[index];
		}

		public int GetCellCount()
		{
			return mCellIDPositionMaps.Count;
		}

		public ICellIDPositionMap GetCellIDPositionMap(int cellID)
		{
			try
			{
				return mCellIDPositionMaps[cellID - 1];
			}
			catch (Exception)
			{
				return null;
			}
		}

		ICellIDPositionMap IPcrStructure.GetCellIDPositionMap(string cellPosition)
		{
			try
			{
				return mCellPositionIDMaps[cellPosition];
			}
			catch (Exception)
			{
				return null;
			}
		}

		IPcrCell IPcrStructure.GetCellFromCellIDMap(ICellIDPositionMap cellIDMap)
		{
			int index = cellIDMap.ModuleIndex - 1;
			List<ISubModule> subModules = mPcrModules[index].SubModules;
			int index2 = (cellIDMap.SubModuleIndex - 1) % subModules.Count;
			return subModules[index2].PcrCells[cellIDMap.CellIndex];
		}

		List<IPcrCell> IPcrStructure.GetPcrCellInExperiment(string experimentName)
		{
			IPcrExperiment pcrExperiment = ((IPcrStructure)this).FindExpriment(experimentName);
			return pcrExperiment.GetPcrCellsInExperiment();
		}

		IPcrExperiment IPcrStructure.FindExpriment(string experimentName)
		{
			foreach (IPcrExperiment mPcrExperiment in mPcrExperiments)
			{
				if (mPcrExperiment.Name.CompareTo(experimentName) == 0)
				{
					return mPcrExperiment;
				}
			}
			return null;
		}

		public List<IPcrExperiment> FindExperimentWithSystemType(PcrSystemType systemType)
		{
			List<IPcrExperiment> list = new List<IPcrExperiment>();
			if (mPcrExperiments != null)
			{
				for (int i = 0; i < mPcrExperiments.Count; i++)
				{
					if (mPcrExperiments[i].SystemType == systemType)
					{
						list.Add(mPcrExperiments[i]);
					}
				}
			}
			return list;
		}

		public void RemoveNotMatchExperiments(PcrSystemType systemType)
		{
			List<IPcrExperiment> list = new List<IPcrExperiment>();
			if (mPcrExperiments == null)
			{
				return;
			}
			for (int i = 0; i < mPcrExperiments.Count; i++)
			{
				if (mPcrExperiments[i].SystemType != systemType)
				{
					list.Add(mPcrExperiments[i]);
				}
			}
			foreach (IPcrExperiment item in list)
			{
				mPcrExperiments.Remove(item);
			}
		}

		IPcrExperiment IPcrStructure.CreateNewExperiment(string experimentName, PcrSystemType systemType)
		{
			IConfigurationService configurationService = (IConfigurationService)mPcrDataFile.Application.GetService("ConfigurationService");
			IPcrExperiment pcrExperiment;
			switch (systemType)
			{
			case PcrSystemType.Allele:
				pcrExperiment = new AlleleExperiment(this);
				break;
			case PcrSystemType.GeneScan:
				pcrExperiment = new HRMExperiment(this);
				break;
			default:
				pcrExperiment = new PcrExperiment(this);
				break;
			}
			pcrExperiment.Name = experimentName;
			pcrExperiment.TemperatureControlType = configurationService.DefaultTemperatureControlType;
			pcrExperiment.TubeVolume = configurationService.DefualtReactionVolume;
			pcrExperiment.IsUseHotLid = configurationService.DefaultUseHotLid;
			pcrExperiment.HotLidSettingTemperature = configurationService.HotLidTemperature;
			pcrExperiment.Operator = configurationService.DefaultOperator;
			pcrExperiment.Assessor = configurationService.DefaultAssessor;
			pcrExperiment.DefaultLabelString = configurationService.LabelString;
			pcrExperiment.IsLabelSortIncrease = configurationService.IsLabelIncrease;
			pcrExperiment.LabelInterval = configurationService.LabelInterval;
			pcrExperiment.LabelStartValue = configurationService.LabelStartValue;
			pcrExperiment.LabelBits = configurationService.LabelBits;
			pcrExperiment.IsHideParameterPanel = configurationService.IsHideParameterPanel;
			pcrExperiment.CellDisplaySortStyle = configurationService.CellDisplaySortStyle;
			pcrExperiment.MultiCellPlacementStyle = configurationService.MultiTubeCellPlaceStyle;
			pcrExperiment.IndexInTab = 0;
			IProjectProperty[] projectPropertyArray = configurationService.ProjectConfiguration.GetProjectPropertyArray();
			for (int i = 0; i < projectPropertyArray.Length; i++)
			{
				if (projectPropertyArray[i].ProjectSystemType == pcrExperiment.SystemType)
				{
					pcrExperiment.ProjectProjerties.Add(projectPropertyArray[i]);
				}
			}
			pcrExperiment.Status = PcrExperimentStatus.Ready;
			pcrExperiment.RunningStatus.IsRunning = false;
			pcrExperiment.RunningStatus.IsRestart = false;
			pcrExperiment.ResultFile = string.Empty;
			pcrExperiment.Password = string.Empty;
			pcrExperiment.HasPassword = false;
			pcrExperiment.PrintOptions = new PrintOptions();
			pcrExperiment.PrintOptions.DefaultReportTemplateFile = configurationService.PrintConfiguration.DefaultReportTemplateFile;
			pcrExperiment.PrintOptions.EachPageReportCount = configurationService.PrintConfiguration.EachPageReportCount;
			pcrExperiment.PrintOptions.PatientReportTitle = configurationService.PrintConfiguration.PatientReportTitle;
			pcrExperiment.IsAvailable = false;
			pcrExperiment.IsDisplayed = false;
			pcrExperiment.SplitterHorizontalRate = configurationService.SplitterHorizontalDefaultRate;
			pcrExperiment.SplitterVerticalRate = configurationService.SplitterVerticalDefaultRate;
			pcrExperiment.CurveColorStyle = configurationService.CurveColorStyle;
			mPcrExperiments.Add(pcrExperiment);
			return pcrExperiment;
		}

		void IPcrStructure.RemoveExperiments(List<string> moduleID, PcrModuleState pcrModuleState)
		{
			List<IPcrExperiment> list = new List<IPcrExperiment>();
			foreach (IPcrExperiment mPcrExperiment in mPcrExperiments)
			{
				if (mPcrExperiment.ModuleState != pcrModuleState)
				{
					continue;
				}
				switch (pcrModuleState)
				{
				case PcrModuleState.Multiple:
					list.Add(mPcrExperiment);
					break;
				case PcrModuleState.Single:
					if (moduleID[0].CompareTo(mPcrExperiment.PcrModules[0].ModuleID) == 0)
					{
						list.Add(mPcrExperiment);
					}
					break;
				}
			}
			foreach (IPcrExperiment item in list)
			{
				mPcrExperiments.Remove(item);
			}
		}

		List<int> IPcrStructure.GetCellIDsFromSubModule(int subModuleID)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < mCellIDPositionMaps.Count; i++)
			{
				if (mCellIDPositionMaps[i].SubModuleIndex == subModuleID)
				{
					list.Add(i + 1);
				}
			}
			return list;
		}
	}
}
